 /*
 * @name: 导出精英数据
 * @Author: along
 * @Date: 2019-07-16
 * @Last Modified by: along
 * @Last Modified time: 2020-07-16
 */
<template>
    <div class="container exportEliteData">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">搜索精英:</p>
                    <el-input
                        v-model="head.stuagent_keyword"
                        placeholder="可搜精英邀请码或手机号或姓名"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                        :style="{ width: '240px' }"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">所在学校/省市搜索:</p>
                    <el-cascader
                        placeholder="请选择"
                        :options="provideList"
                        filterable
                        clearable
                        change-on-select
                        expand-trigger="click"
                        v-model="head.school"
                        @change="getTableData(1)"
                        size="small"
                        :props="{
                            label: 'name',
                            value: 'id',
                            children: 'children'
                        }"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">时间筛选:</p>
                    <el-date-picker
                        v-model="head.date"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        :style="{ width: '300px' }"
                    />
                </div>
                <el-button
                    type="primary"
                    size="small"
                    style="margin-bottom: 10px"
                    @click="getTableData()"
                >查询</el-button>
            </div>
            <div class="header-right">
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="fnExportData()"
                    >
                        导出数据
                    </el-button>
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="list.tableDataList"
            height="100%"
            style="width: 100%;"
            border
            ref="exportEliteData"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="true_name"
                label="精英姓名"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="phone"
                label="精英手机号"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="130"
            />
            <el-table-column
                prop="agent_level"
                label="精英等级"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    E{{ scope.row.agent_level }}
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="所在学校"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="city_name"
                label="所在省市"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="120"
            >
                <template slot-scope="scope">
                    {{ scope.row.province_name + '-' + scope.row.city_name }}
                </template>
            </el-table-column>
            <el-table-column
                prop="invite_code"
                label="邀请码"
                show-overflow-tooltip
                align="center"
            />
            <el-table-column
                prop="s_order_count"
                label="精英自营订单数"
                show-overflow-tooltip
                align="center"
                width="150"
                sortable="custom"
            />
            <el-table-column
                prop="s_tbk_order_count"
                label="精英淘客订单数"
                show-overflow-tooltip
                align="center"
                width="150"
                sortable="custom"
            />
            <el-table-column
                prop="c_order_count"
                label="一级粉丝自营订单数"
                show-overflow-tooltip
                align="center"
                width="180"
                sortable="custom"
            />
            <el-table-column
                prop="c_tbk_order_count"
                label="一级粉丝淘客订单数"
                show-overflow-tooltip
                align="center"
                width="180"
                sortable="custom"
            />
            <el-table-column
                prop="cc_order_count"
                label="二级粉丝自营订单数"
                show-overflow-tooltip
                align="center"
                width="180"
                sortable="custom"
            />
            <el-table-column
                prop="cc_tbk_order_count"
                label="二级粉丝淘客订单数"
                show-overflow-tooltip
                align="center"
                width="180"
                sortable="custom"
            />
            <el-table-column
                prop="s_order_money"
                label="精英自营消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="160"
                sortable="custom"
            />
            <el-table-column
                prop="s_tbk_order_money"
                label="精英淘客消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="160"
                sortable="custom"
            />
            <el-table-column
                prop="c_order_money"
                label="一级粉丝自营消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="190"
                sortable="custom"
            />
            <el-table-column
                prop="c_tbk_order_money"
                label="一级粉丝淘客消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="190"
                sortable="custom"
            />
            <el-table-column
                prop="cc_order_money"
                label="二级粉丝自营消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="190"
                sortable="custom"
            />
            <el-table-column
                prop="cc_tbk_order_money"
                label="二级粉丝淘客消费金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                width="190"
                sortable="custom"
            />
            <!--<el-table-column
                prop="order_count"
                label="累计订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :render-header="renderHeader"
                :width="130"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="fnGetOrder(scope.row)"
                    >{{ scope.row.order_count }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="order_money"
                label="累计GMV"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :render-header="renderHeader2"
                :width="120"
                sortable="custom"
            />-->
            <el-table-column
                prop="rebate_money"
                label="累计收益"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :render-header="renderHeader3"
                :width="120"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ Number(scope.row.rebate_money).toFixed(2) }}
                </template>
            </el-table-column>
            <el-table-column
                prop="fans_count"
                label="一级粉丝"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="grand_fans_count"
                label="二级粉丝"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="total_fans_count"
                label="总粉丝数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="agent_join_time"
                label="成为精英时间"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                :width="150"
                sortable="custom"
            >
                <template slot-scope="scope">
                    {{ scope.row.agent_join_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 订单数 -->
        <!-- <el-dialog
            title="订单详情"
            width="718px"
            :visible.sync="dialogVisible"
            :close-on-click-modal="false"
        >
            <el-table
                :data="orderList"
                height="100%"
                style="width: 100%;max-height:550px"
                border
            >
                <el-table-column
                    type="index"
                    width="80"
                    label="序号"
                    align="center"
                />
                <el-table-column
                    prop=""
                    label="--"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
            </el-table>
        </el-dialog> -->
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import {getDate} from '@/assets/js/utils';
export default {
    mixins: [back],
    components: {},
    data () {
        return {
            // orderList: [],
            // dialogVisible: false,
            provideList: [],
            head: {
                stuagent_keyword: '',
                school: '',
                date: ''
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            list: {
                tableDataList: []
            },
            authResult: {},
            filterData: {
                sort_field: '',
                sort_type: ''
            },
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '数据分析'},{title: '导出精英数据'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
        this.getSchoolTree();
    },
    methods: {
        /**
         * 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/statistic_stuagent%5Cget_statistic_list', {
                page_id: page || this.page.now_page,
                stuagent_keyword: this.head.stuagent_keyword,
                province_id: this.head.school.length > 0 ? this.head.school[0] : '',
                city_id: this.head.school.length > 1 ? this.head.school[1] : '',
                school_id: this.head.school.length > 2 ? this.head.school[2] : '',
                statistic_start_time: this.head.date === null || this.head.date == '' ? '' : getDate(new Date(this.head.date[0])),
                statistic_end_time: this.head.date === null || this.head.date == '' ? '' : (getDate(new Date(this.head.date[1])) + 86400),
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.$set(this.list, 'tableDataList', resp.data.list);
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 获取省+市+学校三级联动数据
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree', {},resp=>{
                if (resp.code == 1) {
                    this.provideList = resp.data.tree;
                } else if (resp.code == 0) {
                    this.$message({
                        type: 'waring',
                        message: resp.msg
                    });
                }
            });
        },

        /**
         * 获取订单详情
         */
        fnGetOrder (row) {
            let routeData = this.$router.resolve({
                name: 'OrderList',
                query: {
                    shop_type: 0,
                    rebate_stuagent_no: row.agent_no
                }
            });

            window.open(routeData.href, '_blank');
        },

        /**
         * 导出数据
         */
        fnExportData () {
            let base = this.$baseUrl;
            let ticket = localStorage.getItem('sessionTicket');
            let url = `${base}/student/statistic_stuagent%5Cexport_statistic_list?stuagent_keyword=${this.head.stuagent_keyword}&province_id=${this.head.school.length > 0 ? this.head.school[0] : ''}&city_id=${this.head.school.length > 1 ? this.head.school[1] : ''}&school_id=${this.head.school.length > 2 ? this.head.school[2] : ''}&statistic_start_time=${this.head.date === null || this.head.date == '' ? '' : getDate(new Date(this.head.date[0]))}&statistic_end_time=${this.head.date === null || this.head.date == '' ? '' : (getDate(new Date(this.head.date[1])) + 86400)}&sort_field=${this.filterData.sort_field}&sort_type=${this.filterData.sort_type}&__ticket=${ticket}`;

            window.open(url);
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 1,
                descending: 0
            };

            let sortFieldMap = {
                agent_join_time: 0,
                // order_count: 1,
                // order_money: 2,
                rebate_money: 3,
                s_order_count: 4,
                s_tbk_order_count: 10,
                c_order_count: 6,
                c_tbk_order_count: 12,
                cc_order_count: 8,
                cc_tbk_order_count: 14,
                s_order_money: 5,
                s_tbk_order_money: 11,
                c_order_money: 7,
                c_tbk_order_money: 13,
                cc_order_money: 9,
                cc_tbk_order_money: 15
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData(1);
        },

        /**
         * 设置表头_累计订单数
         */
        renderHeader(h, {}){
            return h('span', {}, [
                h('span', {}, '累计订单数'),
                h('el-popover', { props: { placement: 'top-start', width: '240', trigger: 'hover', content: '同时也是该数据的定义：包含该精英自己以及其一二级粉丝的总订单数' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader2(h, {}){
            return h('span', {}, [
                h('span', {}, '累计GMV'),
                h('el-popover', { props: { placement: 'top-start', width: '240', trigger: 'hover', content: '同时也是该数据的定义：包含该精英自己以及其一二级粉丝的总GMV' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        renderHeader3(h, {}){
            return h('span', {}, [
                h('span', {}, '累计收益'),
                h('el-popover', { props: { placement: 'top-start', width: '240', trigger: 'hover', content: '同时也是该数据的定义：该精英本人获得的所有分佣相关的金额之和' }}, [
                    h('i', { slot: 'reference', class:'el-icon-question'}, '')
                ])
            ]);
        },

        /**
         * 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .exportEliteData{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .el-dialog__body .el-table__body-wrapper {
            height: auto !important;
            max-height: 500px !important;
        }
    }
</style>
